#include "alarm.h"

Alarm::Alarm( QWidget * parent, Qt::WFlags f) 
	: QDialog(parent, f)
{
#ifdef Q_WS_WIN
	path = QCoreApplication::applicationDirPath();
#else 
	path = QDir::homePath ()+"/.qalarm";
#endif	
	setupUi(this);	
	alarmEdit->setDisplayFormat("HH:mm:ss");
	dateEdit->setDisplayFormat("dd/MMMM/yyyy");
	init();	
	connect(alarmSave, SIGNAL(clicked()), this, SLOT(save()));
	connect(radioOnce, SIGNAL(clicked()), this, SLOT(enable()));
	connect(radioOnceWeek, SIGNAL(clicked()), this, SLOT(enable()));
	connect(radioWeekdays, SIGNAL(clicked()), this, SLOT(enable()));
	connect(radioAlldays, SIGNAL(clicked()), this, SLOT(enable()));
	setWindowIcon(QIcon(path+"/images/alarm.png"));
}

void Alarm::save()
{
	int radio = 0;	
 	Utils utils;
 	utils.saveFile(path+"/data/alarm", alarmEdit->time().toString("HH:mm:ss"));
 	utils.saveFile(path+"/data/date", dateEdit->date().toString("dd/MMMM/yyyy"));
 	utils.saveFile(path+"/data/day",QString::number(daysList->currentIndex()));
 	if (radioOnce->isChecked()) radio = 0;
 	if (radioOnceWeek->isChecked()) radio = 1;
 	else if (radioWeekdays->isChecked()) radio = 2;
 	else if (radioAlldays->isChecked()) radio = 3;
 	utils.saveFile(path+"/data/repeat", QString::number(radio));
 	hide();	
	Utils::changed = true;	
}

void Alarm::enable()
{   	
	if (radioOnce->isChecked()) {
		daysList->setEnabled(false);
		dateEdit->setEnabled(true);
	}
	else if (radioOnceWeek->isChecked()) {
		daysList->setEnabled(true);	
		dateEdit->setEnabled(false);	
	}
	else if (radioWeekdays->isChecked()) {
		daysList->setEnabled(false);
		dateEdit->setEnabled(false);
	}
	else if (radioAlldays->isChecked()) {
		daysList->setEnabled(false);
		dateEdit->setEnabled(false);
	}
}

void Alarm::init()
{	
	Utils utils;
	alarmEdit->setTime(QTime::fromString(utils.readFile(path+"/data/alarm"), "HH:mm:ss"));
	dateEdit->setDate(QDate::fromString(utils.readFile(path+"/data/date"), "dd/MMMM/yyyy"));	
	QStringList list;
	list << "Monday"<<"Tuesday"<<"Wednesday"<<"Thursday"<<"Friday"<<"Saturday"<<"Sunday";	
	daysList->addItems(list);
	daysList->setCurrentIndex(utils.readFile(path+"/data/day").toInt());	
	daysList->setEnabled(false);
	dateEdit->setEnabled(false);	
	int radio = utils.readFile(path+"/data/repeat").toInt();
	if (0 == radio) {
		radioOnce->setChecked(true); 
		dateEdit->setEnabled(true);	
		
	}
	else if (1 == radio) {
		radioOnceWeek->setChecked(true);		
		daysList->setEnabled(true);	
	}
 	else if (2 == radio) radioWeekdays->setChecked(true);
 	else radioAlldays->setChecked(true); 	
}

  

